package homeworks.cjt2010.homework1;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

public class InternalSystemTests {

	public static void main(String[] args) {

		// class Airplane
		// testAirplane();

		// class Airport
		// testAirport();

		// class Passengers
		// testPassenger();

		// class FlightClass
		// testFlightClass();

		// class FlightStatus
		// testFlightStatus();

		// my own test about Date, Calendar API classes
		// testMyDate();

		// class Flight
		// testFlight();

		// class Ticket
		// testTicket();

		// class NoSuchFlightException
		// testNoSuchFlightException();

		// testEnums();

		// testMapToStringMethod();

		// testTimeZone();

		// System.out.println(new Date());
		// generateADateWrappedInCalendar();

		// VERY_IMPORTANT_METHOD();

		// compareTimes();


		// System.out.println(getSystemAirportCurrentTime());

	}

	static Airport systemAirport = Airport.MSK; // GMT+1
	private static final int MILLIS_IN_HOUR = 3600000;

	public static boolean compareAirportTimes(Airport airport,
			Date inputDate) {

		return false;
	}

	public static Date getSystemAirportCurrentTime() {
		Date loadingMachineCurrentTime = new Date();
		int offsetLoadingMachineCurrentTime = TimeZone.getDefault()
				.getRawOffset()
				/ MILLIS_IN_HOUR;
		// System.out.println(loadingMachineCurrentTime);
		// System.out.println(offsetLoadingMachineCurrentTime);

		int offsetSystemAirportCurrentTime = TimeZone.getTimeZone(
				systemAirport.getTimeZone()).getRawOffset()
				/ MILLIS_IN_HOUR;

		// System.out.println(offsetSystemAirportCurrentTime);

		int differenceOffsets = offsetSystemAirportCurrentTime
				- offsetLoadingMachineCurrentTime;

		Date systemAirportCurrentTime = new Date(loadingMachineCurrentTime
				.getTime()
				+ (differenceOffsets * MILLIS_IN_HOUR));

		// System.out.println(differenceOffsets);
		// System.out.println(systemAirportCurrentTime);

		return systemAirportCurrentTime;

	}

	public static void compareTimes() {
		Airplane currentAirplane = new Airplane("BG Air", 120, 100);

		Date currentDate = new Date();
		System.out.println(currentDate);
		Airport systemAirport = Airport.BGY; // GMT+1
		
		TimeZone systemTimeZone = TimeZone.getTimeZone(systemAirport
				.getTimeZone());
		int systemTimeZoneOffset = systemTimeZone.getRawOffset();
		System.out.println(systemTimeZoneOffset / 3600000);

		Date GMTBasedCurrentDate = new Date(currentDate.getTime()
				- systemTimeZoneOffset);
		System.out.println(GMTBasedCurrentDate);
		System.out.println("--------------------------------\n");

		Airport sourceAirport = Airport.LTN; // GMT+0
		Calendar calendar1 = Calendar.getInstance();
		calendar1.set(2010, 03, 28, 12, 38);
		Date sourceDate = calendar1.getTime();
		System.out.println(sourceDate);
		TimeZone sourceTimeZone = TimeZone.getTimeZone(sourceAirport
				.getTimeZone());
		int sourceTimeZoneOffset = sourceTimeZone.getRawOffset();
		System.out.println(sourceTimeZoneOffset / 3600000);

		Date GMTBasedSourceDate = new Date(sourceDate.getTime()
				- sourceTimeZoneOffset);
		System.out.println(GMTBasedSourceDate);

		System.out.println("--------------------------------\n");

		Airport destinationAirport = Airport.VAR; // GMT+2
		Calendar calendar2 = Calendar.getInstance();
		calendar2.set(2010, 03, 29, 10, 25);
		Date destinationDate = calendar2.getTime();
		System.out.println(destinationDate);
		TimeZone destinationTimeZone = TimeZone.getTimeZone(destinationAirport
				.getTimeZone());
		int destinationTimeZoneOffset = destinationTimeZone.getRawOffset();
		System.out.println(destinationTimeZoneOffset / 3600000);

		Date GMTBasedDestinationDate = new Date(destinationDate.getTime()
				- destinationTimeZoneOffset);
		System.out.println(GMTBasedDestinationDate);

		System.out.println("--------------------------------\n");
		System.out.println("My task is to convert source and destination dates"
				+ " to the GMT format of the current Airport");

		System.out.println(currentDate + "\n");
		int differenceSystemSourceTZOffset = systemTimeZoneOffset
				- sourceTimeZoneOffset;
		Date convertedSourceDate = new Date(sourceDate.getTime()
				+ differenceSystemSourceTZOffset);
		System.out.println(convertedSourceDate + "\n");

		int differenceSystemDestinationTZOffset = systemTimeZoneOffset
				- destinationTimeZoneOffset;
		Date convertedDestinationDate = new Date(destinationDate.getTime()
				+ differenceSystemDestinationTZOffset);
		System.out.println(convertedDestinationDate);

		System.out.println(currentDate.before(convertedSourceDate));
		System.out.println(currentDate.before(convertedDestinationDate));

	}

	public static void VERY_IMPORTANT_METHOD() {
		final int MILLIS_IN_HOUR = 3600000;
		Calendar voo = Calendar.getInstance();
		int gmtOffset = voo.get(Calendar.ZONE_OFFSET);
		System.out.println((gmtOffset / MILLIS_IN_HOUR) + " hours");

		Calendar loo = Calendar.getInstance(TimeZone.getTimeZone("GMT+3"));
		int gmtOffset2 = loo.get(Calendar.ZONE_OFFSET);
		System.out.println(gmtOffset2 / MILLIS_IN_HOUR);

		System.out.println(TimeZone.getTimeZone("GMT+3").getRawOffset()
				/ MILLIS_IN_HOUR);

		voo.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		System.out.println(voo.getTimeZone().getRawOffset() / MILLIS_IN_HOUR);

		voo.setTimeZone(TimeZone.getTimeZone("GMT-6"));
		System.out.println(voo.getTimeZone().getRawOffset() / MILLIS_IN_HOUR);

		Date date = new Date();
		voo.setTime(date);
		System.out.println(voo.getTimeZone().getRawOffset() / MILLIS_IN_HOUR);
	}

	public static void generateADateWrappedInCalendar() {

		try {
			Airport at = Airport.VAR;
			TimeZone tz = TimeZone.getTimeZone(at.getTimeZone());

			// Locale loc= new Locale("en","GB");
			Locale loc = new Locale("bg", "BG");

			String stringDate = "11.06.07, 13:20";
			SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy, HH:mm",
					loc);
			DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, loc);
			System.out.println(df.parse(stringDate));

			Date date = sdf.parse(stringDate);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.setTimeZone(tz);

			System.out.println("Today is " + cal.getTime());

		} catch (ParseException e) {
			System.out.println("Exception :" + e);
		}

	}

	public static void testDateCalendarTimeZoneClasses() {
		Date date = new Date();
		long dateInMilliSeconds = date.getTime() - 2 * (3600 * 1000);

		System.out.println(date);
		// System.out.println(dateInMilliSeconds);
		Date newDate = new Date(dateInMilliSeconds);
		System.out.println(newDate);
		System.out.println(newDate.before(date));
		// testTime();

		System.out.println("-------------------");
		// Date myDate= new Date();
		// System.out.println(TimeZone.getDefault());
		//		
		// System.out.println(TimeZone.getDefault().getOffset(myDate.getTime()));

		System.out.println("-------------------");

		// Calendar hack= Calendar.getInstance(TimeZone.getTimeZone("GMT+2"));
		// hack.get

		Calendar voo = Calendar.getInstance();
		int gmtOffset = voo.get(Calendar.ZONE_OFFSET);
		System.out.println(gmtOffset);

		Calendar loo = Calendar.getInstance(TimeZone.getTimeZone("GMT+3"));
		int gmtOffset2 = loo.get(Calendar.ZONE_OFFSET);
		System.out.println(gmtOffset2);

		System.out.println(TimeZone.getTimeZone("GMT+3").getRawOffset());

		System.out.println("----------------- START BOO --------------------");

		System.out.println("----------------- FINISH BOO --------------------");

		// System.out.println(voo.getTimeZone());
		// TimeZone vooTimeZone=voo.getTimeZone();
		// long vooTime= voo.getTimeInMillis();
		// System.out.println(vooTimeZone.getOffset(vooTime));

		System.out.println("-------------------");

		Calendar cal = Calendar.getInstance();
		System.out.println(cal.getTime());
		System.out.println(cal.getTimeInMillis());
		System.out.println(cal.getTimeZone());
		System.out.println(cal.ZONE_OFFSET);
		System.out.println(Calendar.ZONE_OFFSET);

	}

	// 13
	public static void testTime() {
		Date date1 = new Date();
		System.out.println(date1);
		Calendar c = Calendar.getInstance();
		System.out.println(c.getTimeZone());
	}

	// 12
	public static void testTimeZone() {
		// TimeZone tz1 = TimeZone.getDefault();
		// System.out.println(tz1);
		//
		// System.out.println( TimeZone.SHORT);
		// System.out.println(TimeZone.LONG);
		//		
		// System.out.println(Arrays.toString(TimeZone.getAvailableIDs()));

		TimeZone tz2 = TimeZone.getTimeZone("GMT");
		TimeZone tz3 = TimeZone.getTimeZone("GMT+0");
		TimeZone tz4 = TimeZone.getTimeZone("GMT+3");
		TimeZone tz5 = TimeZone.getTimeZone("GMT-4");
		TimeZone tz6 = TimeZone.getTimeZone("GMT+0010");
		System.out.println(tz2);
		System.out.println(tz3);
		System.out.println(tz4);
		System.out.println(tz5);
		System.out.println(tz6);

		// System.out.println(tz3.getOffset(new Date().));
	}

	// 11
	public static void testMapToStringMethod() {

		Map<Integer, String> someMap = new HashMap<Integer, String>();
		someMap.put(2, "two");
		someMap.put(15, "fifteen");
		someMap.put(10, "ten");
		someMap.put(7, "seven");

		System.out.println(someMap.toString());

	}

	// 10
	public static void testEnums() {

		for (FlightStatus fs : FlightStatus.values()) {
			System.out.println(fs + ": " + fs.ordinal());
		}
		System.out.println(FlightStatus.values().length);

		// -----------

		Airport airp = Airport.SOF;
		// for (Airport arp : airp.values()) {
		// System.out.println(arp);
		// }
		System.out.println("-------------");
		System.out.println(airp);
		// System.out.println(airp.LTN); // allowed, but not recommended

		System.out.println("-------------");
		Airport airp2 = Airport.DTM;
		Airport airp3 = Airport.EIN;
		Airport airp4 = Airport.DTM;
		System.out.println(airp2 == airp3);
		System.out.println(airp2.equals(airp3));

		System.out.println(airp2 == airp4);
		System.out.println(airp2.equals(airp4));

		// Enum
	}

	// 09
	public static void testNoSuchFlightException() {
		NoSuchFlightException nf1 = new NoSuchFlightException();
		NoSuchFlightException nf2 = new NoSuchFlightException("brrrr");
		NoSuchFlightException nf3 = new NoSuchFlightException(new Exception());
		System.out.println(nf1.toString());
		System.out.println(nf2.toString());
		System.out.println(nf3.toString());
	}

	// 08
	public static void testTicket() {
		// flight
		Date depTime = new Date();
		Date arrTime = new Date();
		Airplane ap = new Airplane("Concord", 110, 40);

		Flight flight = new Flight(depTime, arrTime, Airport.DTM, Airport.LTN,
				ap);

		Passenger pass = new Passenger("Velko Mishev", "109832", 12000L);

		Ticket ticket = new Ticket(flight, 469.3, FlightClass.BUSSINESS, 24,
				pass);

		System.out.println(ticket.getPerson());
		System.out.println(ticket.getFlight());
		System.out.println(ticket);
	}

	// 07
	public static void testFlight() {
		Date depTime = new Date();
		Date arrTime = new Date();
		Airplane ap = new Airplane("Concord", 110, 40);

		Flight flight = new Flight(depTime, arrTime, Airport.DTM, Airport.LTN,
				ap);
		System.out.println(flight);
	}

	// 06
	public static void testMyDate() {
		Date date1 = new Date();
		System.out.println(date1);

		Calendar cal = Calendar.getInstance();
		System.out.println(cal.getTime());

		Locale loc = new Locale("en");
		System.out.println(loc.getCountry());
		System.out.println(loc.getLanguage());

		Locale loc2 = new Locale("bg", "BG");
		System.out.println(loc2);
		Calendar cal2 = Calendar.getInstance(loc2);
		System.out.println(cal2.getTime());

		System.out.println("-------");

		System.out.println("------------");
		System.out.println(Locale.getDefault());
		System.out.println(Locale.GERMANY);
		System.out.println(Locale.ITALY);
		// Locale.setDefault(Locale.CHINA);
		System.out.println(Locale.getDefault());

		System.out.println(new Date());

		System.out.println("------------");

		System.out.println("------------");
		Calendar c = Calendar.getInstance();
		Date d = c.getTime();

		Locale loc10 = new Locale("it", "IT");
		DateFormat df10 = DateFormat.getDateInstance(DateFormat.LONG, loc10);
		System.out.println(df10.format(d));

		Locale loc11 = new Locale("fr", "FR");
		DateFormat df11 = DateFormat.getDateInstance(DateFormat.FULL, loc11);

		System.out.println(df11.format(d));

	}

	// 05
	public static void testFlightStatus() {
		for (FlightStatus fs : FlightStatus.values()) {
			System.out.println(fs);
		}
	}

	// 04
	public static void testFlightClass() {
		FlightClass fc1 = FlightClass.FIRST;
		System.out.println(fc1);
		System.out.println(fc1.getPrice());
	}

	// 03
	public static void testPassenger() {
		Passenger pass = new Passenger("Velko Mishev", "109832", 12000L);
		Passenger pass2 = new Passenger("Maria Koleva", "3924", 14900L);
		System.out.println(pass);
		System.out.println(pass2);
		System.out.println(pass.equals(pass2));
		System.out.println("pass2 name= " + pass2.getName());
		System.out.println("pass2 miles= " + pass2.getMiles());
	}

	// 02
	public static void testAirport() {
		Airport airport1 = Airport.SOF;
		System.out.println("airport1--> " + airport1);
		System.out.println("airport1 name= " + airport1.getName());
		System.out.println("airport1 tZone= " + airport1.getTimeZone());

		System.out.println("----------------\n");
		for (Airport ae : Airport.values()) {
			System.out.println(ae);
		}
	}

	// 01
	public static void testAirplane() {
		Airplane airplane = new Airplane("Lufthansa", 130, 100);
		Airplane airplane2 = new Airplane("Concord", 110, 40);

		System.out.println(airplane);
		System.out.println(airplane2);
	}

}
